package org.javatribe.calculator.common;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Wenxin on 2017/9/16.
 */
public class BaseFrameBuilder {

    private static final Logger logger = LoggerFactory.getLogger(BaseFrameBuilder.class);

    private final List<Handler> handlers = new ArrayList<>();

    private Class<? extends BaseFrame> clazz;

    public BaseFrameBuilder(Class<? extends BaseFrame> clazz) {
        this.clazz = clazz;
    }

    public BaseFrameBuilder addHandler(Handler handler) {
        handlers.add(handler);
        return this;
    }

    public BaseFrame build() {
        try {
            BaseFrame instant = clazz.newInstance();
            instant.init();
            instant.addHandlers(handlers);
            return instant;
        } catch (InstantiationException e) {
            logger.error("build base frame failure", e);
        } catch (IllegalAccessException e) {
            logger.error("build base frame failure", e);
        }
        return null;
    }
}
